package com.bjsxt.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.controller.BaseController;
import com.bjsxt.common.util.ConstantFinal;
import com.bjsxt.common.util.DateFormatUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.user.pojo.Admin;
import com.bjsxt.user.service.IUserService;

@Controller
@RequestMapping("/back/admin/")
public class AdminBackController extends BaseController
{
	@Resource
	private IUserService userService;
	@RequestMapping("index.htm")
	public String index()
	{
		return "/back/index";
	}
	//退出
	@RequestMapping("loginOut.htm")
	public String loginOut(HttpServletRequest req)
	{
		HttpSession session = req.getSession();
		session.removeAttribute("admin");
		session.removeAttribute("lastLoginTime");
		session.setAttribute("info", "成功退出");
		return "redirect:/adminLogin.htm";
	}
	//左边菜单界面
	@RequestMapping("menu.htm")
	public String menu()
	{
		ConstantFinal.LOGGER_SYS.info("----menu");
		return "/back/menu";
	}
	//管理员添加页面
	@RequestMapping("adminInsert.htm")
	public String adminInsert()
	{
		return "/back/adminInsert";
	}
	//管理员添加提交value = "/adminsInsertSubmit.htm",produces="text/html;charset=UTF-8"
	@RequestMapping(value = "/adminInsertSubmit.htm",produces="text/html;charset=UTF-8")
	public @ResponseBody String adminInsertSubmit(HttpServletRequest req,Admin admin)
	{
		ConstantFinal.LOGGER_SYS.info("要添加的管理员信息为：{}",admin);
		admin.setPassword(this.encryPasswordUtil.getNewEncryPass(admin.getPassword()));
		admin.setCreateTime(new Date());
		admin.setUpdateTime(new Date());
		admin.setLastLoginTime(new Date());
		JSONObject insertResJson = this.userService.insertOneAdmin(admin);
		String info = insertResJson.getString("info");
		JSONObject returnJson = this.returnJson(req, info);
		
		return returnJson.toJSONString();
	}
	//管理员列表页面
	@RequestMapping("adminList.htm")
	public String adminList(HttpServletRequest req)
	{
		//根据请求来得到分页信息
		PageInfoUtil pageInfo = this.getPageInfoUtil(req);
		Map<String,Object> condMap=new HashMap<String,Object>();
		//接收管理员列表顶部的请求数据
		String keyword = req.getParameter("keyword");
		String status = req.getParameter("status");
		
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		
		String st = req.getParameter("st");
		String ed = req.getParameter("ed");
		if(st==null)
		{
			st="";
		}
		st=st.trim();
		if(ed==null)
		{
			ed="";
		}
		ed=ed.trim();
		
		if(!"".equals(st) && !"".equals(ed))
		{
			try
			{
				Date stDate = DateFormatUtil.parseIntoDateTime(st);
				Date edDate = DateFormatUtil.parseIntoDateTime(ed);
				condMap.put("stDate", stDate);
				condMap.put("edDate", edDate);
				
			} catch (Exception e)
			{
			}
		}
		
		List<Admin> adminList = userService.findListAdmin(pageInfo, condMap);
		
		ConstantFinal.LOGGER_SYS.info("---分页信息：{}",pageInfo);
		
		req.setAttribute("pageInfo", pageInfo);
		req.setAttribute("adminList", adminList);
		req.setAttribute("keyword", keyword);
		req.setAttribute("status", status);
		req.setAttribute("st", st);
		req.setAttribute("ed", ed);
		return "/back/adminList";
	}
}
